package ezy.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Html;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import ezy.widget.R;

/**
 * Created by ezy on 14-12-11.
 */
public class MoreTextView extends TextView {
    private static final int[] R_styleable_MoreTextView = {
            R.attr.more,
            R.attr.less
    };

    private int _maxLines;
    private BufferType _bufferType = BufferType.NORMAL;
    private CharSequence _text;
    private Spanned _more;
    private Spanned _less;

    public MoreTextView(Context context) {
        super(context);
        setup();
    }

    public MoreTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
        setup();
    }

    public MoreTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
        setup();
    }
    private void init(Context context, AttributeSet attributeSet) {
        TypedArray a = context.obtainStyledAttributes(attributeSet, R_styleable_MoreTextView);
        try {
            String txtMore = a.getString(0);
            String txtLess = a.getString(1);

            if (!TextUtils.isEmpty(txtMore)){
                _more = Html.fromHtml(txtMore);
            }
            if (!TextUtils.isEmpty(txtLess)){
                _less = Html.fromHtml(txtLess);
            }
        } finally {
            a.recycle();
        }
    }

    public void setMore(Spanned more) {
        _more = more;
    }
    public void setLess(Spanned less) {
        _less = less;
    }


    @Override
    public void setMaxLines(int maxlines) {
        _maxLines = maxlines;
        setup();
        requestLayout();
    }

    @Override
    public void setText(final CharSequence text, BufferType type) {
        _text = text;
        _bufferType = type;
        setup();
        super.setText(text, type);
    }

    private void setup() {
        if (_listener == null || _maxLines < 1 || _text == null) {
            return;
        }
        getViewTreeObserver().addOnGlobalLayoutListener(_listener);
    }

    private ViewTreeObserver.OnGlobalLayoutListener _listener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            getViewTreeObserver().removeGlobalOnLayoutListener(this);

            if (getLineCount() < _maxLines) {
                return;
            }
            expandable(wrap(_text, _less), wrap(summarize(), _more));
        }
    };

    private Spanned wrap(CharSequence content, Spanned spanned) {
        if (spanned == null) {
            return new SpannedString(content);
        }
        SpannableStringBuilder ss = new SpannableStringBuilder();
        ss.append(content);
        ss.append("\n");
        ss.append(spanned);
        return ss;
    }
    private SpannableString summarize() {
        int end = getLayout().getLineEnd(_maxLines - 1);
        return new SpannableString(getText().subSequence(0, end).toString());
    }

    private void expandable(final Spanned content, final Spanned summary) {
        setTextInternal(summary);
        setOnClickListener(new View.OnClickListener() {
            boolean expand = false;
            @Override
            public void onClick(View view) {
                setTextInternal(expand ? content : summary);
                expand = !expand;
            }
        });
    }

    private void setTextInternal(CharSequence text) {
        super.setText(text, _bufferType);
    }
}
